package com.sdk.utils;

import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.*;
import org.influxdb.dto.Point.Builder;

import java.util.HashMap;
import java.util.Map;

public class InfluxDBConnUtils {

    private String username;//用户名
    private String password;//密码
    private String openurl;//连接地址
    private String database;//数据库

    private InfluxDB influxDB;

    public InfluxDBConnUtils(String openurl, String database,String username,String password){
        this.username = username;
        this.password = password;
        this.openurl = openurl;
        this.database = database;
        this.influxDB = influxDbBuild();
    }

    /**连接时序数据库；获得InfluxDB**/
    public InfluxDB influxDbBuild() {
        if (influxDB==null) {
            influxDB = InfluxDBFactory.connect(openurl, username, password);
            Pong pong = influxDB.ping();
            if(pong != null){
                System.out.println("pong：" + pong + ",连接成功！");
            }else{
                System.out.println("连接失败");
                return null;
            }
            influxDB.createDatabase(database);
        }
        return influxDB;
    }

    /**
     * 设置数据保存策略
     * defalut 策略名 /database 数据库名/ 30d 数据保存时限30天/ 1  副本个数为1/ 结尾DEFAULT 表示 设为默认的策略
     */
    public void createRetentionPolicy(){
        String command = String.format("CREATE RETENTION POLICY \"%s\" ON \"%s\" DURATION %s REPLICATION %s DEFAULT",
                "defalut", database, "30d", 1);
        this.query(command);
    }

    /*
     * 查询
     * @param command 查询语句
     * @return
     * */
    public QueryResult query(String command) {
        Query query = new Query(command, database);
        QueryResult a = influxDB.query(query);
        return a;
    }

    public static void main(String args[]) {
        Map<String,String> map = new HashMap<String,String>();
        map.put("test","1");
        Map<String,Object> map1= new HashMap<String,Object>();
        map1.put("ABC","123");

        InfluxDBConnUtils influxDB = new InfluxDBConnUtils("http://127.0.0.1:8086", "test","influxdb","influxdb");
        influxDB.influxDbBuild();
        try {
            influxDB.insert("test",map,map1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 插入
     * @param measurement 表
     * @param tags 标签
     * @param fields 字段
     */
    public void insert(String measurement, Map<String, String> tags, Map<String, Object> fields){
        Builder builder = Point.measurement(measurement);
        builder.tag(tags);
        builder.fields(fields);
        influxDB.write(database, "", builder.build());
    }

    /**
     * 批量插入
     * @param batchPoints
     */
    public void write(BatchPoints batchPoints){
        influxDB.write(batchPoints);
    }

    /**
     * 删除
     * @param command 删除语句
     * @return 返回错误信息
     */
    public String deleteMeasurementData(String command){
        QueryResult result = influxDB.query(new Query(command, database));
        return result.getError();
    }

    /**
     * 创建数据库
     * @param dbName
     */
    public void createDB(String dbName){
        influxDB.createDatabase(dbName);
    }

    /**
     * 删除数据库
     * @param dbName
     */
    public void deleteDB(String dbName){
        influxDB.deleteDatabase(dbName);
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getOpenurl() {
        return openurl;
    }

    public void setOpenurl(String openurl) {
        this.openurl = openurl;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

}
